﻿Imports System.ComponentModel

Public Class frmFinancialSetupVoucherType
    Private m_VoucherGroupList As List(Of ClsTVoucherGroup)
    Private m_State As Int16 '当前编辑状态
    Private m_Saving As Boolean '当前是否正在保存
    Private m_Dirty As Boolean '当前数据是否修改
    Private m_Row As Integer '当前列表中选择的行

    Private m_CurGroup As ClsTVoucherGroup '保存当前正在处理的类对象

    Private m_Broker As ClsBroker = New ClsBroker() '新增或修改后，发出事件广播

    Private Sub frmFinancialSetupVoucherType_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '注册订阅事件
        AddHandler ClsBroker.OnCustomCtrolContentChange, AddressOf DataDirty

        '载入全部凭证类别集合
        LoadGroupData()
        '数据装入控件列表
        LoadListView()
        '状态初值 
        m_State = 0 '浏览
        m_Saving = False
        m_Dirty = False
        m_Row = -1
        '控件状态
        CheckControlState()

    End Sub

    Private Sub CheckControlState()
        '检查按钮
        CheckButtonState()
        '检查文本框
        CheckTextBoxState()
    End Sub

    Private Sub CheckTextBoxState()
        Select Case m_State
            Case 0
                Me.txtGroupName.ReadOnly = True
                Me.txtShortName.ReadOnly = True
            Case 1
                Me.txtGroupName.ReadOnly = IIf(m_Saving, True, False)
                Me.txtShortName.ReadOnly = IIf(m_Saving, True, False)
            Case 2
                Me.txtShortName.ReadOnly = IIf(m_Saving, True, False)
                Me.txtGroupName.ReadOnly = IIf(m_Saving, True, False)
        End Select
    End Sub
    Private Sub CheckButtonState()
        Select Case m_State
            Case 0 '浏览
                Me.btnAdd.Enabled = True
                Me.btnModify.Enabled = IIf(m_CurGroup Is Nothing, False, True)
                Me.btnSave.Enabled = False
                Me.btnClose.Enabled = True
                Me.btnAdd.Text = "新建"
                Me.btnModify.Text = "编辑"
            Case 1 '新建
                Me.btnAdd.Enabled = True
                Me.btnAdd.Text = "取消新建"

                Me.btnModify.Enabled = False
                Me.btnSave.Enabled = False
                Me.btnClose.Enabled = True
            Case 2 '编辑
                Me.btnAdd.Enabled = False
                Me.btnModify.Enabled = True
                Me.btnModify.Text = "取消编辑"
                Me.btnSave.Enabled = False
                Me.btnClose.Enabled = True
        End Select
    End Sub
    Private Sub DataDirty(ByVal sender As Object, ByVal e As CustomControlChangeNotifyEventArg)
        If m_State > 0 Then
            m_Dirty = True

            Me.btnSave.Enabled = True
        End If
    End Sub

    Private Sub LoadGroupData()
        If m_VoucherGroupList Is Nothing Then
            m_VoucherGroupList = New List(Of ClsTVoucherGroup)
        End If
        '创建表读取类
        Dim tmpVoucherGroupDb As ClsTVoucherGroupDbAccess = New ClsTVoucherGroupDbAccess(sysUserDataConnectionString, TABLE_NAME_T_VOUCHER_GROUP)
        m_VoucherGroupList = tmpVoucherGroupDb.GetAllRecords(Of ClsTVoucherGroup)
    End Sub

    Private Sub LoadListView()
        If m_VoucherGroupList Is Nothing Then
            Return
        End If

        '设置表头
        InitialLedgerListViewHeder()
        '添加数据
        LoadDataToListView()
    End Sub

    Private Sub LoadDataToListView()
        For Each tmpCls As ClsTVoucherGroup In m_VoucherGroupList

            AddOneClassToListView(tmpCls)
        Next

    End Sub

    Private Sub AddOneClassToListView(ByVal tmpCls As ClsTVoucherGroup)
        Dim tmpItem As ListViewItem = New ListViewItem(tmpCls.GroupName)
        tmpItem.SubItems.Add(tmpCls.ShortName)

        Me.lstVoucherType.Items.Add(tmpItem)
    End Sub

    Private Sub InitialLedgerListViewHeder()
        Dim header1 As ColumnHeader = New ColumnHeader
        With header1
            .Text = "类别名称"
            .Width = 100
            .TextAlign = HorizontalAlignment.Center
            .Name = "hGroupName"
        End With

        Dim header2 As ColumnHeader = New ColumnHeader()
        With header2
            .Text = "类别字号"
            .Width = 60
            .TextAlign = HorizontalAlignment.Left
            .Name = "hShortName"
            '.AutoResize = True
        End With

        With Me.lstVoucherType
            .FullRowSelect = True '行选择模式
            .MultiSelect = True '不允许 多行选择
            .View = View.Details '视图选项：详细模式
            '.Width = Me.FlowLayoutPanel1.Width  '宽度占满窗体
            .HeaderStyle = ColumnHeaderStyle.Nonclickable '标题不可点击
            .Columns.Add(header1)
            .Columns.Add(header2)
        End With

    End Sub

    Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub lstVoucherType_ItemSelectionChanged(sender As Object, e As ListViewItemSelectionChangedEventArgs) Handles lstVoucherType.ItemSelectionChanged
        '取出当前选中
        Dim tmpSelectedIndex As Int16 = -1

        If Me.lstVoucherType.SelectedItems.Count > 0 Then
            tmpSelectedIndex = Me.lstVoucherType.SelectedItems.Item(0).Index
        End If

        If tmpSelectedIndex > -1 AndAlso m_VoucherGroupList IsNot Nothing AndAlso m_VoucherGroupList.Count > 0 Then
            m_Row = tmpSelectedIndex
            m_CurGroup = m_VoucherGroupList.Item(tmpSelectedIndex)
            '类赋值控件
            ClassToControl()
            '检查控件状态
            CheckControlState()
        End If
    End Sub

    Private Sub ClassToControl()
        If m_CurGroup IsNot Nothing Then
            Me.txtGroupName.Text = m_CurGroup.GroupName
            Me.txtShortName.Text = m_CurGroup.ShortName
        Else
            Me.txtGroupName.Text = ""
            Me.txtShortName.Text = ""
        End If
    End Sub

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        If m_State = 1 Then
            '此时是取消新增
            '修改状态
            m_State = 0
            m_Saving = False
            m_Dirty = False
            '类值入控件
            ClassToControl()
            '检查状态
            CheckControlState()
        Else

            '清空控件内容，准备输入
            ClearTextBox()
            '修改状态
            m_State = 1
            m_Saving = False
            m_Dirty = False
            '检查控件状态
            CheckControlState()
        End If
    End Sub

    Private Sub ClearTextBox()
        Me.txtGroupName.Text = ""
        Me.txtShortName.Text = ""
    End Sub

    Private Sub frmFinancialSetupVoucherType_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
        '移除订阅事件
        RemoveHandler ClsBroker.OnCustomCtrolContentChange, AddressOf DataDirty
        '判断当前是否有数据未保存
        If m_State > 0 And m_Dirty Then
            If MessageBox.Show("当前数据未保存，是否需要保存？", "凭证类别", MessageBoxButtons.YesNo) = DialogResult.Yes Then
                '取消关闭
                e.Cancel = True
                '保存当前数据
                SaveCurrentData()
            End If
        End If
    End Sub

    Private Sub SaveCurrentData()
        '控件内容检查 
        If Not ValidControlContent() Then
            Return
        End If
        '判断状态是否合适
        If m_State = 0 Then
            Return
        End If

        '控件内容到类
        ControlToClass()

        '判断是否可以保存
        If m_CurGroup Is Nothing Then
            MessageBox.Show("没有需要保存的凭证类别数据。", "凭证类别")
            Return
        End If

        '修改状态
        m_Saving = True
        m_Dirty = False

        '检查状态
        CheckControlState()

        '类内容到库
        SaveClassToTable()

        '修改状态
        m_State = 0
        m_Saving = False
        m_Dirty = False

        '检查状态
        CheckControlState()
    End Sub

    Private Sub SaveClassToTable()

        '开始保存
        Dim tmpDb As ClsTVoucherGroupDbAccess = New ClsTVoucherGroupDbAccess(sysUserDataConnectionString)
        Dim tmpId As Integer
        Dim tmpE As TransEventArg


        If m_State = 1 Then
            '新增
            tmpId = tmpDb.InsertGroup(m_CurGroup)
            If tmpId = 0 Then
                MessageBox.Show("保存凭证类别失败。")
                Return
            Else
                m_CurGroup.GroupId = tmpId

                '添加到集合中
                If m_VoucherGroupList Is Nothing Then
                    m_VoucherGroupList = New List(Of ClsTVoucherGroup)
                End If
                m_VoucherGroupList.Add(m_CurGroup)
                '添加到控件列表
                AddOneClassToListView(m_CurGroup)
                '发布广播，通知新增
                tmpE = New TransEventArg()
                tmpE.Msg = "InsertVoucherGroup"
                tmpE.Obj = m_CurGroup

                m_Broker.BroadCastEvent(tmpE)

            End If
        Else
            '修改
            tmpId = tmpDb.SaveGroup(m_CurGroup)
            If tmpId = 0 Then
                MessageBox.Show("保存凭证类别失败。")
                Return
            Else
                '修改控件列表
                EditOneClassInListView(m_Row)

                '发布广播，通知修改
                tmpE = New TransEventArg()
                tmpE.Msg = "EditVoucherGroup"
                tmpE.Obj = m_CurGroup

                m_Broker.BroadCastEvent(tmpE)

                '修改所有凭证中的字
                Dim tmpVoucherDetailDb As ClsTVoucherDbAccess = New ClsTVoucherDbAccess(sysUserDataConnectionString)
                Dim tmpResult As Integer = tmpVoucherDetailDb.UpdateVoucherGroup(m_CurGroup)
                If tmpResult = 0 Then
                    '出错了
                    MessageBox.Show("更新凭证中相应凭证类别时出错。", "保存凭证类别")
                End If
            End If
        End If

    End Sub

    Private Sub EditOneClassInListView(ByVal row As Integer)
        If row > Me.lstVoucherType.Items.Count - 1 Then
            Return
        End If

        If m_CurGroup Is Nothing Then
            Return
        End If

        '修改指定行内容
        Dim tmpItem As ListViewItem = Me.lstVoucherType.Items(row)
        tmpItem.Text = m_CurGroup.GroupName
        tmpItem.SubItems(1).Text = m_CurGroup.ShortName

    End Sub

    Private Sub ControlToClass()
        If m_CurGroup Is Nothing Then
            m_CurGroup = New ClsTVoucherGroup()
        End If

        If m_State = 1 Then
            m_CurGroup.GroupId = 0
        End If
        m_CurGroup.GroupName = Me.txtGroupName.Text.Trim()
        m_CurGroup.ShortName = Me.txtShortName.Text.Trim()
    End Sub

    Private Function ValidControlContent() As Boolean
        If String.IsNullOrEmpty(Me.txtGroupName.Text) OrElse String.IsNullOrEmpty(Me.txtShortName.Text) Then
            MessageBox.Show("凭证类别中的名称及字号均不能为空。", "凭证类别")
            Return False
        Else
            '判断字号长度
            If Me.txtShortName.Text.Trim().Length > 2 Then
                MessageBox.Show("凭证类别的字号长度不能大于2个字符。", "凭证类别")
                Return False
            Else
                Return True
            End If
        End If

    End Function

    Private Sub btnModify_Click(sender As Object, e As EventArgs) Handles btnModify.Click
        If m_State = 2 Then
            '取消编辑
            '类内容入控件
            ClassToControl()
            '修改状态
            m_State = 0
            m_Saving = False
            m_Dirty = False
        Else
            '修改状态
            m_State = 2
            m_Saving = False
            m_Dirty = False
        End If

        '检查控件状态
        CheckControlState()
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        SaveCurrentData()
    End Sub

    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        '删除当前凭证类别
        If m_CurGroup Is Nothing Then
            Return
        End If

        '第一，检查当前类别是否被使用过
        Dim tmpVoucherDb As ClsTVoucherDbAccess = New ClsTVoucherDbAccess(sysUserDataConnectionString)

        If tmpVoucherDb.GetIfHaveVoucherGroup(m_CurGroup) > 0 Then
            '已被使用，不可删除
            MessageBox.Show("当前类别已被使用，不能删除。", "删除凭证类别")
            Return
        End If
        '第二，如果没有被使用过，则可以删除
        Dim tmpVoucherGroupDb As ClsTVoucherGroupDbAccess = New ClsTVoucherGroupDbAccess(sysUserDataConnectionString)
        Dim tmpDelInt As Integer = tmpVoucherGroupDb.DeleteVoucherGroup(m_CurGroup)

        If tmpDelInt > 0 Then

            '通知其他窗口
            Dim tmpE As TransEventArg = New TransEventArg()
            tmpE.Msg = "DeleteVoucherGroup"
            tmpE.Obj = m_CurGroup
            m_Broker.BroadCastEvent(tmpE)

            '从集合中删除
            m_VoucherGroupList.Remove(m_CurGroup)

            '从列表中删除
            Me.lstVoucherType.Items.Remove(Me.lstVoucherType.Items(m_Row))

            '修改当前窗体变量
            m_CurGroup = Nothing
            m_Row = -1
            Me.txtGroupName.Text = ""
            Me.txtShortName.Text = ""

        Else
            MessageBox.Show("删除当前凭证类别时出错。", "删除凭证类别")

        End If

    End Sub

End Class